﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Sys
{
    public class GrantController : PageInfoBase
    {
        [ActionFilter(IsLogin = true)]
        public ActionResult Index(String type,String id, String keyCode)
        {
            ViewBag.type = type;
            ViewBag.id = id;
            ViewBag.keyCode = keyCode;           
            return View(BasePage.V("Sys/Grant"));
        }
        //权限设置：角色 用户数据读取
        [ActionFilter(IsLogin = true)]
        public JsonResult Data(String type, String id, String keyCode)
        {
            PageInfo pg = isBrowse(type, id, keyCode);
            if (pg == null)
            {
                return BasePage.Json(ResultData.Error(WebMessage.NoBrowse));
            }
            String sql = "select" +
                        "    t1.smenuid" + BasePage.bd.CharAdd + "(case t0.soperate when 'ALL' then '' else t0.soperate end) as \"id\"," +
                        "    (case when t0.soperate='ALL' then t1.sparentmenuid else t0.smenuid end)  as \"pId\"," +
                        "    t0.sgranddescript as \"name\"," +
                        "    (case when t2.smenuid is null then 'false' else 'true' end)  as \"checked\" " +
                        "from" +
                        "  tb_sys_Grand t0" +
                        "  inner join tb_sys_menu t1 on t0.smenuid=t1.smenuid and t1.itype=1" +
                        "  left join " +
                        (type.Equals("GrantRole") ? "tb_sys_rolegrand" : "tb_sys_usergrand") + " t2 on " +
                        (type.Equals("GrantRole") ? "srolecode" : "iuserid") + "='" + id +
                        "' and t0.smenuid=t2.smenuid and t0.soperate=t2.soperate " +
                        "order by \"pId\",t1.iorder,\"id\"";
            BasePage.bd.ColumnNameToLower = false;
            String str = JsonConvert.SerializeObject(BasePage.bd.ExecuteTable(sql));
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data=JsonConvert.DeserializeObject<List<Dictionary<String,Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }

        [ActionFilter(IsLogin = false)]
        public JsonResult SaveData(String type, String id, String keyCode, String menuId)
        {
            PageInfo pg = isBrowse(type, id, keyCode);
            if (pg == null)
            {
                return BasePage.Json(ResultData.Error(WebMessage.NoBrowse));
            }
            BaseData bd = new BaseData();

            String[] sql = new String[2];
            if (type.Equals("GrantRole"))
            {
                sql[0] = String.Format("delete tb_sys_rolegrand where srolecode='{0}'", id);
                sql[1] = String.Format("insert into tb_sys_rolegrand (srolecode,smenuid,soperate) select '{0}',smenuid,soperate from tb_sys_Grand where smenuid{1}(case soperate when 'ALL' then '' else soperate end) in ({2})", id, bd.CharAdd, menuId);
            }
            else
            {
                sql[0] = String.Format("delete tb_sys_usergrand where iuserid='{0}'", id);
                sql[1] = String.Format("insert into tb_sys_usergrand (iuserid,smenuid,soperate) select '{0}',smenuid,soperate from tb_sys_Grand where smenuid{1}(case soperate when 'ALL' then '' else soperate end) in ({2})", id, bd.CharAdd, menuId);
            }
            bool t = bd.ExecuteSQL(sql);
            if (t)
                return BasePage.Json(ResultData.Code("0000", WebMessage.SaveSuccess));
            else
                return BasePage.Json(ResultData.Code("0001", WebMessage.SaveFailure));
        }
    }
}